//
//  ViewController.m
//  14_UICollectionViewTest
//
//  Created by 徐昊 on 16/8/2.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"
#import "ImageCollectionViewCell.h"
#import "LabelCollectionReusableView.h"
#import "LabelAndCollectionReusableView.h"

#define kCollectionViewName @"reuse"
#define kCollectionViewHeaderName @"reuseHeader"
#define kCollectionViewNoCell @"header"
#define kRandomColor [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1]
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 创建collectionView
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds) - 10) / 3, (CGRectGetWidth(self.view.bounds) - 10) / 3);
    // 行间距
    _layout.minimumLineSpacing = 0;
    // 列间距
    _layout.minimumInteritemSpacing = 5;
    // 滚动方向
    _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 区头大小
    _layout.headerReferenceSize = CGSizeMake(0, 50);
    // 区尾大小
    _layout.footerReferenceSize = CGSizeMake(20, 20);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:_layout];
    [self.view addSubview:collectionView];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewName];
    [collectionView registerClass:[LabelCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderName];
    [collectionView registerClass:[LabelAndCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewNoCell];
    collectionView.backgroundColor = [UIColor whiteColor];

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 100;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewName forIndexPath:indexPath];
    cell.backgroundColor = kRandomColor;
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        // 2. 自定义一个头视图
        LabelAndCollectionReusableView *reuse = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewNoCell forIndexPath:indexPath];
        return reuse;
    } else {
        LabelCollectionReusableView *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderName forIndexPath:indexPath];
        reuseView.backgroundColor = [UIColor redColor];
        // 1. 通过上边距实现视图添加(UICollectionView本质是UIScrollView)
        //    collectionView.contentInset = UIEdgeInsetsMake(300, 0, 0, 0);
        //    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -295, self.view.bounds.size.width, 290)];
        //    view.backgroundColor = [UIColor greenColor];
        //    [collectionView addSubview:view];
        return reuseView;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(0, 400);
    } else {
        return CGSizeMake(0, 50);
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
